<!--
《生产任务》

- 仅列出根节点：parent_handle_newTask = '0', queryParams.parentUuid: '0'
- 点击【详情】，新窗口打开

-->


<template>
  <div class="app-container">

    <!-- 检索条件 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="是否完工" prop="sysYesNoDone">
        <el-select v-model="queryParams.sysYesNoDone" placeholder="请选择是否完工" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="定额级别" prop="mesDictQuotaLevel">
        <el-select v-model="queryParams.mesDictQuotaLevel" placeholder="请选择定额级别" clearable size="small">
          <el-option
            v-for="dict in dict.type.mes_dict_quota_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单位时长" prop="quotaPerMinutes">
        <el-input
          v-model="queryParams.quotaPerMinutes"
          placeholder="请输入单位时长"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 批量操作 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:tasks:add']"
          v-if="show1"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:tasks:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:tasks:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" stripe
              :data="taskList"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="expand"  align="center" width="50">
        <template slot-scope="props">
          <el-form label-position="left"  class="demo-table-expand">
            <el-form-item label="审核人：">
              <span>{{ props.row.confirmUserName }}</span>
            </el-form-item>
            <el-form-item label="审核结果：">
              <dict-tag :options="dict.type.oa_dict_confirm_status" :value="props.row.oaDictConfirmStatus"/>
            </el-form-item>
            <el-form-item label="审核时间：">
              <span>{{ props.row.confirmDt }}</span>
            </el-form-item>
            <el-form-item label="审核意见：">
              <span>{{ props.row.confirmNote }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="物料名称" align="center"
                       prop="itemName" width="280" :show-overflow-tooltip="true"
                       v-if="columns[0].visible"/>
      <el-table-column label="物料编号" align="center"
                       prop="itemNum" v-if="columns[1].visible"
                       :show-overflow-tooltip="true"/>

      <el-table-column label="数量" align="center" v-if="columns[2].visible">
        <template slot-scope="scope">
          <span> {{ Math.floor((scope.row.taskQty)) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="单据编号" align="center" prop="docNum" width="150" v-if="columns[3].visible"/>
      <el-table-column label="单据日期" align="center" prop="docDt" width="180" v-if="columns[4].visible"/>
      <el-table-column label="selectPart_done" align="center" prop="attentionUserName" v-if="columns[5].visible"/>
      <el-table-column label="审核人" align="center" prop="confirmUserName" v-if="columns[6].visible"/>

      <el-table-column label="工艺编号" align="center" prop="processDocNum" v-if="columns[7].visible"/>
      <el-table-column label="工艺版本" align="center" prop="processVersionNum" v-if="columns[8].visible"/>

      <el-table-column label="订单编号" align="center" prop="docNum" width="150" v-if="columns[9].visible"/>
      <el-table-column label="订单日期" align="center" prop="docDt" width="120" v-if="columns[10].visible"/>
      <el-table-column label="客户名称" align="center" prop="custName" width="160" v-if="columns[11].visible"/>
      <el-table-column label="是否完工" align="center" prop="sysYesNoDone" v-if="columns[12].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.sysYesNoDone"/>
        </template>
      </el-table-column>
      <el-table-column label="定额级别" align="center" prop="mesDictQuotaLevel" v-if="columns[13].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_dict_quota_level" :value="scope.row.mesDictQuotaLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="单位时长" align="center" prop="quotaPerMinutes" v-if="columns[14].visible"/>
      <el-table-column label="总时长" align="center" prop="quotaTotalMinutes" v-if="columns[15].visible"/>
      <el-table-column label="审核状态" align="center" prop="oaDictConfirmStatus" v-if="columns[16].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.oa_dict_confirm_status" :value="scope.row.oaDictConfirmStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="168">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showDialog_quota(scope.row)"
            v-hasPermi="['mes:tasks:quota']"
          >定额
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-set-up"
            @click="win_show_details(scope.row)"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="showDialog_confirm(scope.row)"
            v-hasPermi="['mes:process:remove']"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 生产任务定额 -->
    <el-dialog v-dialogDrag v-dialogDragWidth :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules_quota" label-width="100px">
        <el-form-item label="定额级别" prop="mesDictQuotaLevel">
          <el-select v-model="form.mesDictQuotaLevel" placeholder="请选择定额级别" style="width: 240px">
            <el-option
              v-for="dict in dict.type.mes_dict_quota_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单位时长" prop="quotaPerMinutes">
          <el-input v-model="form.quotaPerMinutes" placeholder="请输入单位时长" style="width: 240px"/>
        </el-form-item>
        <el-form-item label="总时长" prop="quotaTotalMinutes">
          <el-input v-model="form.quotaTotalMinutes" placeholder="请输入总时长" style="width: 240px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm_task_edit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <select-orderDetail ref="component_selectOrderDetail" @ok="selectOrderDetail_done"/>
    <confirm ref="component_confirm"  @ok="confirm_done" />

    <!--

      对话框（）：下达生产派工

      -->
    <el-dialog v-dialogDrag v-dialogDragWidth :title="dialog_title_genDispatch"
               :visible.sync="dialog_visible_genDispatch"
               width="600px" append-to-body>

      <el-alert style="margin-bottom: 20px;"
                :title="form_genDispatch.fkTaskUuid"
                type="success">
      </el-alert>

      <el-form ref="form_genDispatch" :model="form_genDispatch" :rules="rules_genDispatch"
               label-width="90px">

        <el-input v-model="form_genDispatch.fkTaskUuid" v-show="false"/>

        <el-row>
          <el-col :span="12">
            <el-form-item label="任务类型" prop="mesDictDispatchType">
              <el-select v-model="form_genDispatch.mesDictDispatchType" placeholder="请选择任务类型" clearable size="small" :style="{width: '100%'}">
                <el-option
                  v-for="dict in dict.type.mes_dict_dispatch_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标题" prop="docTitle">
              <el-input v-model="form_genDispatch.docTitle" placeholder="请输入标题" :style="{width: '100%'}"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="生产批次" prop="docNum">
              <el-input v-model="form_genDispatch.docNum" placeholder="请输入生产派工的批次号" :style="{width: '100%'}"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时间" prop="docDt">
              <el-date-picker clearable size="small"
                              v-model="form_genDispatch.docDt"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择时间（若使用当前时间请留空）" :style="{width: '100%'}">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="docNote">
              <el-input v-model="form_genDispatch.docNote" autosize type="textarea" placeholder="请输入内容" :style="{width: '100%'}"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="经办人" prop="fkAttentionUserId">
              <el-input v-model="form_genDispatch.fkAttentionUserId" v-show="false"/>
              <el-input placeholder="请选择经办人" v-model="form_genDispatch.selectedAttentionUser" class="input-with-select">
                <el-button @click="dialog_show_selectUser" :style="{width: '100%'}" slot="append" icon="el-icon-search"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm_genDispatch">确 定</el-button>
        <el-button @click="dialog_hide_genDispatch">取 消</el-button>
      </div>

    </el-dialog>
    <select-user ref="component_selectUser" @ok="selectUser_done"/>

  </div>
</template>

<script>
import {
  listTask,
  getTask,
  delTask,
  addTask,
  updateTask,
} from "@/api/mes/tasks";
import selectUser from "../../system/user/selectUser";
import {genDispatch} from "@/api/mes/dispatch";
import confirm from "../../oa/confirm";
import selectOrderDetail from "../../crm/crmOrders/selectOrderDetail";

export default {
  name: "Task",
  dicts: ['mes_dict_dispatch_type','oa_dict_confirm_status','sys_yes_no','mes_dict_quota_level'],
  components: { selectOrderDetail,confirm,selectUser },
  data() {
    return {
      show1:false,
      dialog_visible_genDispatch: false,
      dialog_title_genDispatch: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentUuid: '0',
        fkProcessDetailUuid: null,
        orderDetailUuid: null,
        selectedAttentionUser:null,
        fkAttentionUserId:null,
        sysYesNoDone: null,
        mesDictQuotaLevel: null,
        quotaPerMinutes: null,
        quotaTotalMinutes: null
      },
      // 表单参数
      form: {
        selectedAttentionUser:null,
        fkAttentionUserId:null,
      },
      form_genDispatch: {
        fkAttentionUserId:null,
      },
      // 表单校验
      rules_quota:{
        mesDictQuotaLevel: [
          {required: true, message: "请输入定额级别", trigger: "change"}
        ],
        quotaPerMinutes: [
          {required: true, message: "请输入单位时长", trigger: "change"}
        ],
        quotaTotalMinutes: [
          {required: true, message: "请输入总时长", trigger: "change"}
        ],
      },
      rules: {
        fkProcessDetailUuid: [
          {required: true, message: "工艺明细ID不能为空", trigger: "blur"}
        ],
        orderDetailUuid: [
          {required: true, message: "订单明细ID不能为空", trigger: "blur"}
        ],
        sysYesNoDone: [
          {required: true, message: "是否完工不能为空", trigger: "blur"}
        ],
      },
      rules_genDispatch: {
        mesDictDispatchType: [
          {required: true, message: "请选择任务类型", trigger: "change"}
        ],
        docTitle: [
          {required: true, message: "请输入标题", trigger: "change"}
        ],
        docNum: [
          {required: true, message: "请输入生产批次", trigger: "change"}
        ],
        docDt: [
          {required: true, message: "请选择时间", trigger: "change"}
        ],
        fkAttentionUserId: [
          {required: true, message: "请选择经办人", trigger: "change"}
        ],
      },
      // 列信息
      columns: [
        { key: 0, label: `物料名称`, visible: true },
        { key: 1, label: `物料编号`, visible: true },
        { key: 2, label: `数量`, visible: true },
        { key: 3, label: `单据编号`, visible: true },
        { key: 4, label: `单据日期`, visible: true },
        { key: 5, label: `经办人`, visible: true },
        { key: 6, label: `审核人`, visible: true },
        { key: 7, label: `工艺编号`, visible: true },
        { key: 8, label: `工艺版本`, visible: true },
        { key: 9, label: `订单编号`, visible: true },
        { key: 10, label: `订单日期`, visible: true },
        { key: 11, label: `客户名称`, visible: true },
        { key: 12, label: `是否完工`, visible: true },
        { key: 13, label: `定额级别`, visible: true },
        { key: 14, label: `单位时长`, visible: true },
        { key: 15, label: `总时长`, visible: true },
        { key: 16, label: `审核状态`, visible: true }
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 打开对话框：审核 */
    showDialog_confirm(row){
      console.log(row)
      let obj = {
        tableName: 'mes_tasks',
        uuidColumn: 'task_uuid',
        fkUuid: row.taskUuid,
        attentionUserNickName: row.attentionUserName,
        oaDictConfirmStatus:row.oaDictConfirmStatus,
        msg: `请审核生产任务单, 单据编号：${row.docNum}`
      }
      this.$refs.component_confirm.showDialog_confirm(obj);

    },

    confirm_done(){
      this.getList()
    },
    /** 查询生产任务列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskUuid: null,
        fkProcessDetailUuid: null,
        orderDetailUuid: null,
        selectedAttentionUser:null,
        fkAttentionUserId:null,
        sysYesNoDone: null,
        mesDictQuotaLevel: null,
        quotaPerMinutes: null,
        quotaTotalMinutes: null
      };
      this.resetForm("form");
    },
    reset_genDispatch() {
      this.form_genDispatch = {
        fkAttentionUserId: null,
        mesDictDispatchType:null,
        docTitle:null,
        docDt:null,
        docNum:null,
      };
      this.resetForm("form_genDispatch");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      // this.ids = selection.map(item => item.taskUuid)
      // this.single = selection.length!==1
      // this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产任务";
    },
    /** 修改按钮操作 */
    showDialog_quota(row) {
      this.reset();
      const taskUuid = row.taskUuid || this.ids
      getTask(taskUuid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "定额";
        this.form.selectedOrderDetail =
          `物料名称：${this.form.itemName}，物料编码：${this.form.itemNum}，订单编号：${this.form.docNum}`
        console.log(this.form.selectedOrderDetail)
      });
    },
    /** 提交按钮 */
    submitForm_task_edit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.taskUuid != null) {
            updateTask(this.form).then(response => {
              console.log(response)
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const taskUuids = row.taskUuid || this.ids;
      this.$modal.confirm('是否确认删除单据编号为"' + row.docNum + '",单据日期为"' + row.docDt + '"的生产任务？','删除确认').then(function () {
        return delTask(taskUuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/task/export', {
        ...this.queryParams
      }, `mes_task.xlsx`)
    },

    // 窗口：显示 - 查看详情
    win_show_details(r) {
      this.$router.push(`/mes/task-details/${r.taskUuid}`);
    },


    /** 下达生产派工 */
    dialog_show_genDispatch(row) {
      this.reset_genDispatch()
      if (row != null && row.taskUuid) {
        this.form_genDispatch.fkTaskUuid = row.taskUuid
      } else {
        console.log(this.form.parentUuid)
      }
      this.dialog_visible_genDispatch = true;
      this.dialog_title_genDispatch = "下达生产派工"


    },

    dialog_hide_genDispatch() {
      this.dialog_visible_genDispatch = false;
      this.reset_genDispatch();
    },


    /** 提交按钮 - 下达生产派工 */
    submitForm_genDispatch() {
      this.$refs["form_genDispatch"].validate(valid => {
        if (valid) {
          genDispatch(this.form_genDispatch).then(response => {

            if (response.code != 200) {

            }

            this.$notify({
              title: '成功',
              message: '生产派工已经成功下达。',
              type: 'success',
              duration: 0
            });

            this.dialog_hide_genDispatch()
          });
        }
      });
    },
    /** 显示对话框：选择订单 */
    dialog_show_selectOrderDetail() {
      this.$refs.component_selectOrderDetail.dialog_show_selectOrderDetail();
    },

    /** 选择订单明细完成 */
    selectOrderDetail_done(order) {
      this.form.fkOrderDetailUuid = order.row.orderDetailUuid
      this.form.selectedOrderDetail =
        `物料名称：${order.row.itemName}，物料编码：${order.row.itemNum}，订单编号：${order.row.docNum}`
      console.log(this.form.selectedOrderDetail)
    },

    /** 打开授权用户表弹窗 */
    dialog_show_selectUser() {
      this.$refs.component_selectUser.show();
    },

    /** 选择用户完成 */
    selectUser_done(user) {
      console.log(user)
      this.form.fkAttentionUserId = user.userId
      this.form.selectedAttentionUser = `${user.nickName}（用户名：${user.userName}，ID：${user.userId}）`

      this.form_genDispatch.fkAttentionUserId = user.userId
      this.form_genDispatch.selectedAttentionUser = `${user.nickName}（用户名：${user.userName}，ID：${user.userId}）`
    },
  }
};
</script>





































